﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAO;
using DTO;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Repository;

namespace QLDaiLy
{
    public partial class TiepNhanDaiLy : XtraUserControl
    {
        public TiepNhanDaiLy()
        {
            InitializeComponent();
            

        }
        private void LoadDanhDanhSachDaiLy()
        {
            DataTable dt = DaiLyDAO.LayDanhSachDaiLy1();
            DatadridView.DataSource = dt;
        }
        public void GiaoDienMacDinh()
        {
            textbTenDaiLy.Text = textbDienThoai.Text = textbDiaChi.Text = textbEmail.Text = null;


        }
        private void TiepNhanDaiLy_Load(object sender, EventArgs e)
        {
            LoadDanhDanhSachDaiLy();
            LoadDanhSachQuan();
            LoadDanhSachLoaiDaiLy();

        }
        private void LoadDanhSachQuan()
        {
            List<QuanDTO> list = new List<QuanDTO>();
            list = QuanDAO.LoadDanhSachQuan();
            lkupQuan.Properties.DataSource = list;
            lkupQuan.Properties.ValueMember = "MaQuan";
            lkupQuan.Properties.DisplayMember = "TenQuan";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenQuan");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lkupQuan.Properties.Columns.Add(col);
        }
        private void LoadDanhSachLoaiDaiLy()
        {
            List<LoaiDaiLyDTO> list = new List<LoaiDaiLyDTO>();
            list = LoaiDaiLyDAO.LoadDanhSachLoaiDaiLy();
            lkupLoaiDaiLy.Properties.DataSource = list;
            lkupLoaiDaiLy.Properties.ValueMember = "MaLoaiDaiLy";
            lkupLoaiDaiLy.Properties.DisplayMember = "TenLoaiDaiLy";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenLoaiDaiLy");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lkupLoaiDaiLy.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private void simpleCheck_CheckedChanged(object sender, EventArgs e)
        {
            advanceCheck.Properties.ValueChecked = false;

        }
        private void advanceCheck_CheckedChanged(object sender, EventArgs e)
        {
            simpleCheck.Properties.ValueChecked = false;
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            TimKiemNangCao frm = new TimKiemNangCao();
            tb.Controls.Add(frm, 0, 1);
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            string ngay, thang, nam;
            ngay = dateNgayTiepNhan.DateTime.Day.ToString();
            thang = dateNgayTiepNhan.DateTime.Month.ToString();
            nam = dateNgayTiepNhan.DateTime.Year.ToString();
            DataTable quiDinh = QuiDinhDAO.DanhSachQuiDinh();
            int quan = lkupQuan.EditValue.GetHashCode();
            int slDaiLyTrongQuan = DaiLyDAO.SoDaiLyToiDaTrongQuan(quan);
            int slDaiLyTheoQD = quiDinh.Rows[0][0].GetHashCode();

            DaiLyDTO dl = new DaiLyDTO();
            dl.MaDaiLy = DaiLyDAO.TaoMaDaiLy();
            dl.TenDaiLy = textbTenDaiLy.Text;
            dl.MaLoaiDaiLy = Convert.ToInt32(lkupLoaiDaiLy.EditValue.ToString());
            dl.MaQuan = int.Parse(lkupQuan.EditValue.ToString());
            dl.DiaChi = textbDiaChi.Text;
            dl.DienThoai = textbDienThoai.Text;
            dl.Email = textbEmail.Text;
            dl.NgayTiepNhan = thang + "/" + ngay + "/" + nam;
            dl.NoCuaDaiLy = 0;
            if (slDaiLyTheoQD <= slDaiLyTrongQuan)
            {
                MessageBox.Show("Số Đại Lý Trong Quận vượt mức qui định. Yêu cầu liên lạc với quản trị hệ thống");
                GiaoDienMacDinh();
            }
            else
            {
                if (DaiLyDAO.TiepNhanDaiLy(dl) == true)
                {
                    MessageBox.Show("Thêm Đại Lý thành công !");
                    // dataGridView.Rows.Add();
                    GiaoDienMacDinh();
                    LoadDanhDanhSachDaiLy();

                }
                else
                {
                    MessageBox.Show("Có lổi khi tiếp nhận đại lý");
                }
            }
        }
        private void gridView1_Click(object sender, EventArgs e)
        {
           // DatadridView view = sender as DatadridView;
            textbTenDaiLy.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["TenDaiLy"]).ToString();
            lkupLoaiDaiLy.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["TenLoaiDaiLy"]).ToString();
            lkupQuan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["TenQuan"]).ToString();
            textbDiaChi.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiaChi"]).ToString();
            textbDienThoai.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DienThoai"]).ToString();
            textbEmail.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Email"]).ToString();
            dateNgayTiepNhan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["NgayTiepNhan"]).ToString();
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            string ngay, thang, nam;
            ngay = dateNgayTiepNhan.DateTime.Day.ToString();
            thang = dateNgayTiepNhan.DateTime.Month.ToString();
            nam = dateNgayTiepNhan.DateTime.Year.ToString();
            DataTable quiDinh = QuiDinhDAO.DanhSachQuiDinh();
            int quan = lkupQuan.EditValue.GetHashCode();
            int slDaiLyTrongQuan = DaiLyDAO.SoDaiLyToiDaTrongQuan(quan);
            int slDaiLyTheoQD = quiDinh.Rows[0][0].GetHashCode();

            DaiLyDTO dl = new DaiLyDTO();
            dl.MaDaiLy = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["MaDaiLy"]).ToString();
            dl.TenDaiLy = textbTenDaiLy.Text;
            dl.MaLoaiDaiLy = Convert.ToInt32(lkupLoaiDaiLy.EditValue.ToString());
            dl.MaQuan = int.Parse(lkupQuan.EditValue.ToString());
            dl.DiaChi = textbDiaChi.Text;
            dl.DienThoai = textbDienThoai.Text;
            dl.Email = textbEmail.Text;
            dl.NgayTiepNhan = thang + "/" + ngay + "/" + nam;
            dl.NoCuaDaiLy = 0;
            if (slDaiLyTheoQD <= slDaiLyTrongQuan)
            {
                MessageBox.Show("Số Đại Lý Trong Quận vượt mức qui định. Yêu cầu liên lạc với quản trị hệ thống");
                GiaoDienMacDinh();
            }
            else
            {
                if (DaiLyDAO.CapNhatDaiLy(dl) == true)
                {
                    MessageBox.Show("Cập nhật thành công !");
                    // dataGridView.Rows.Add();
                    GiaoDienMacDinh();
                    LoadDanhDanhSachDaiLy();

                }
                else
                {
                    MessageBox.Show("Có lổi khi tiếp nhận đại lý");
                }
            }

        }
        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            string ten= tboxTimKiem.Text;
            DataTable tb = DaiLyDAO.TimKiemDaiLyCoBan(ten);
            DatadridView.DataSource = tb;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }
    }
}
